//
//  NHTRouterManager.swift
//  TestIos
//
//  Created by liyadong on 2025/11/12.
//  Copyright © 2025 Liyadong. All rights reserved.
//

import UIKit

@objc(NHTRouterManager)
class NHTRouterManager: NSObject {
    
    /// 单例
    static let shared = NHTRouterManager()
    
    /// 注册所有路由
    @objc static func registerAllRoutes() {
        shared.registerRoutes()
    }
    
    /// 注册所有路由
    private func registerRoutes() {
        // 场景1: 基础页面跳转 - 设置页面
        NHTRouter.registerWithHandler(NHTRouterURL.settings) { params in
            let vc = RouterSettingsViewController()
            NHTRouterTool.pushViewController(vc, with: params)
        }
        
        // 场景2: 带参数的页面跳转 - 用户详情页
        NHTRouter.registerWithHandler(NHTRouterURL.userDetail) { params in
            guard let params = params,
                  let userId = params["userId"] as? String else {
                return
            }
            
            let vc = RouterDetailViewController()
            vc.userId = userId
            vc.userName = params["name"] as? String
            
            // 获取userInfo中的额外信息
            if let userInfo = params[NHTRouterParameterUserInfo] as? [String: Any] {
                vc.extraInfo = userInfo
            }
            
            NHTRouterTool.pushViewController(vc, with: params)
        }
        
        // 场景3: 带多个参数的页面跳转 - 商品详情页
        NHTRouter.registerWithHandler("nht://product/:productId/:category") { params in
            guard let params = params,
                  let productId = params["productId"] as? String,
                  let category = params["category"] as? String else {
                return
            }
            
            let vc = RouterProductViewController()
            vc.productId = productId
            vc.category = category
            NHTRouterTool.pushViewController(vc, with: params)
        }
        
        // 场景4: 带Query参数的跳转
        NHTRouter.registerWithHandler("nht://search") { params in
            guard let params = params else { return }
            
            let vc = RouterDetailViewController()
            vc.userId = params["keyword"] as? String
            vc.userName = params["type"] as? String
            
            var extraInfo: [String: Any] = [:]
            for (key, value) in params {
                if key != NHTRouterParameterURL && 
                   key != NHTRouterParameterCompletion &&
                   key != NHTRouterParameterUserInfo {
                    extraInfo[key] = value
                }
            }
            vc.extraInfo = extraInfo
            
            NHTRouterTool.pushViewController(vc, with: params)
        }
        
        // 场景5: 使用ObjectHandler返回对象
        NHTRouter.registerWithObjectHandler(NHTRouterURL.apiUser) { params -> Any? in
            guard let params = params,
                  let userId = params["userId"] as? String else {
                return nil
            }
            
            // 模拟返回用户数据
            return [
                "userId": userId,
                "userName": "用户\(userId)",
                "avatar": "https://example.com/avatar/\(userId).jpg"
            ]
        }
        
        // 场景6: 带通配符的路由
        NHTRouter.registerWithHandler("nht://wildcard/~") { params in
            guard let params = params,
                  let url = params[NHTRouterParameterURL] as? String else {
                return
            }
            
            let vc = RouterDetailViewController()
            vc.userId = "通配符路由"
            vc.userName = url
            NHTRouterTool.pushViewController(vc, with: params)
        }
        
        print("✅ NHTRouterManager: 所有路由注册完成")
    }
}

